home *** CD-ROM | disk | FTP | other *** search
- /*----------------------------------------------------------------------------
-
- popuputil.c
-
- This reusable module contains miscellaneous popup menu utility routines.
-
- Copyright © 1994-1995, Northwestern University.
-
- ----------------------------------------------------------------------------*/
-
- #include "def.h"
- #include "popuputil.h"
-
-
-
- /*----------------------------------------------------------------------------
- GetPopupPString
-
- Get the text of an item in a Popup Menu control, as a Pascal format
- string.
-
- Entry: ctl = handle to popup menu control.
- item = item number to get, or kCurrentPopupItem for
- the currently selected item.
-
- Exit: str = the item text, as a Pascal format string.
- ----------------------------------------------------------------------------*/
-
- void GetPopupPString (ControlHandle ctl, short item, Str255 str)
- {
- popupPrivateData **data;
-
- data = (popupPrivateData**)(**ctl).contrlData;
- if (item == kCurrentPopupItem) item = GetControlValue(ctl);
- GetMenuItemText((**data).mHandle, item, str);
- }
-
-
-
- /*----------------------------------------------------------------------------
- GetPopupCString
-
- Get the text of an item in a Popup Menu control, as a C format
- string.
-
- Entry: ctl = handle to popup menu control.
- item = item number to get, or kCurrentPopupItem for
- the currently selected item.
-
- Exit: str = the item text, as a C format string.
- ----------------------------------------------------------------------------*/
-
- void GetPopupCString (ControlHandle ctl, short item, char *str)
- {
- GetPopupPString(ctl, item, (StringPtr)str);
- p2cstr((StringPtr)str);
- }
-
-
-
- /*----------------------------------------------------------------------------
- SetPopupItemStyle
-
- Set the text style for one of the items in a popup menu control.
-
- Entry: ctl = handle to popup menu control.
- item = item number to set, or kCurrentPopupItem for
- the currently selected item.
- style = the new text style.
- ----------------------------------------------------------------------------*/
-
- void SetPopupItemStyle (ControlHandle ctl, short item, short style)
- {
- popupPrivateData **data;
-
- data = (popupPrivateData**)(**ctl).contrlData;
- if (item == kCurrentPopupItem) item = GetControlValue(ctl);
- SetItemStyle((**data).mHandle, item, style);
- }
-
-
-
- /*----------------------------------------------------------------------------
- AddPopupItem
-
- Add an item to a popup menu control.
-
- Entry: ctl = handle to popup menu control.
- after = item number after which the new item should be
- inserted, or 0 to insert the new item and the beginning
- of the menu.
- str = text or the new item, in Pascal format.
- ----------------------------------------------------------------------------*/
-
- void AddPopupItem (ControlHandle ctl, short after, Str255 str)
- {
- popupPrivateData **data;
- short curValue;
-
- data = (popupPrivateData**)(**ctl).contrlData;
- InsertMenuItem((**data).mHandle, str, after);
- curValue = GetControlValue(ctl);
- if (after < curValue) SetControlValue(ctl, curValue+1);
- }
-
-
-
- /*----------------------------------------------------------------------------
- DelPopupItem
-
- Delete an item from a popup menu control.
-
- Entry: ctl = handle to popup menu control.
- item = item number to delete, or kCurrentPopupItem to delete
- the currently selected item.
- ----------------------------------------------------------------------------*/
-
- void DelPopupItem (ControlHandle ctl, short item)
- {
- popupPrivateData **data;
- short curValue;
-
- data = (popupPrivateData**)(**ctl).contrlData;
- curValue = GetControlValue(ctl);
- if (item == kCurrentPopupItem) item = curValue;
- DeleteMenuItem((**data).mHandle, item);
- if (item < curValue) SetControlValue(ctl, curValue-1);
- }
-
-
- /*----------------------------------------------------------------------------
- SetPopupValue
-
- Set the value of a popup menu control to the item matching a string.
-
- Entry: ctl = handle to popup menu control.
- str = Pascal format string.
- isNumber = true if string and menu items are numbers.
-
- Exit: function result = new control value, or 0 if no matching
- item.
- ----------------------------------------------------------------------------*/
-
- short SetPopupValue (ControlHandle ctl, Str255 str, Boolean isNumber)
- {
- popupPrivateData **data;
- long checkVal, itemVal;
- MenuHandle menu;
- short numItems, item;
- Str255 tempStr;
-
- data = (popupPrivateData**)(**ctl).contrlData;
- if (isNumber) StringToNum(str, &checkVal);
- menu = (**data).mHandle;
-
- numItems = CountMItems(menu);
- for (item = 1; item <= numItems; item++) {
- GetMenuItemText(menu, item, tempStr);
- if (isNumber) {
- StringToNum(tempStr, &itemVal);
- if (checkVal == itemVal) {
- SetControlValue(ctl, item);
- return item;
- }
- } else if (EqualString(str, tempStr, false, true)) {
- SetControlValue(ctl, item);
- return item;
- }
- }
- return 0;
- }
-
-
-
- /*----------------------------------------------------------------------------
- TrackPopup
-
- Track a click in a typein popup menu control.
-
- Entry: ctl = handle to popup menu control.
- where = location of mouse click, in local coords.
- checkItem = text of the item that should appear checked
- (the contents of the "partner" typein textedit field),
- in Pascal format.
- isNumber = true if menu items are numbers.
-
- Exit: function result = new control value, or 0 if no change.
-
- If the text does not match a menu item, a new item is added
- at the beginning, followed by a separator line. These two extra
- items are deleted before the function returns.
- ----------------------------------------------------------------------------*/
-
- short TrackPopup (ControlHandle ctl, Point where, Str255 checkItem, Boolean isNumber)
- {
- popupPrivateData **data;
- MenuHandle menu;
- long checkVal;
- short i, itemsAdded, part, newValue, oldValue;
- Str255 tempStr;
-
- data = (popupPrivateData **)(**ctl).contrlData;
- menu = (**data).mHandle;
- itemsAdded = 0;
- if (checkItem && *checkItem) {
- oldValue = SetPopupValue(ctl, checkItem, isNumber);
- if (oldValue == 0) {
- if (isNumber) {
- StringToNum(checkItem, &checkVal);
- NumToString(checkVal, tempStr);
- InsertMenuItem(menu, tempStr, 0);
- } else {
- InsertMenuItem(menu, checkItem, 0);
- }
- InsertMenuItem(menu, "\p(-", 1);
- oldValue = 1;
- itemsAdded = 2;
- SetControlValue(ctl, oldValue);
- }
- } else {
- oldValue = 0;
- }
-
- part = TrackControl(ctl, where, (ControlActionUPP)-1);
- newValue = GetControlValue(ctl);
-
- if (part != 0 && oldValue != newValue) {
- newValue = newValue - itemsAdded;
- } else {
- newValue = 0;
- }
- if (itemsAdded) {
- for (i = 0; i < itemsAdded; i++) {
- DeleteMenuItem(menu, 1);
- }
- SetControlValue(ctl, newValue);
- }
- return newValue;
- }
-